#include "Enemy.h"
#include <iostream>

using namespace std;

Enemy::Enemy(String name, OgreOde::World *world, Vector3 position, CraftList craft2)
{
	//char t_name[80];

    mWorld = world;
    //mName = (String) strcat(strcpy(t_name, name.c_str()), "_enemy");
	mName = name;
    mSceneMgr = world->getSceneManager();
    mSpace = world->getDefaultSpace();
    mPosition = position;

	mAirCraftBoids = craft2;

	//Ogre::String naampje = "enemy_" + name;
	Vector3 size(50.0,50.0,50.0);
	mGeom = (OgreOde::Geometry*)new OgreOde::BoxGeometry(size, mWorld, mSpace);
	create_enemy();
}

Enemy::~Enemy()
{
	//delete mEntity;
//	enemy_node->removeAndDestroyAllChildren();
	mSceneMgr->destroySceneNode(mName);
	mSceneMgr->destroyEntity(enemy_entity);
	mBody->~ Body();
	mGeom->~ Geometry();
	printf("Destroyed!\n");
}


void Enemy::create_enemy()
{
	std::cout << "Creating: " << mName << std::endl;
Vector3 size(50.0,50.0,50.0);
	
	
	enemy_entity = mSceneMgr->createEntity("something_" + mName, "ogrehead.mesh");
    enemy_entity->setQueryFlags (1<<2);
    enemy_node = mSceneMgr->getRootSceneNode()->createChildSceneNode(mName);

    enemy_node->attachObject(enemy_entity);
    mBody = new OgreOde::Body(mWorld, mName);
    enemy_node->attachObject(mBody);
    OgreOde::BoxMass mMass(0.5,size);
  //  mMass.setDensity(5.0,size);
  //  mBody->setMass(mMass);
    
    enemy_node->setScale(size.x * 0.02,size.y * 0.02,size.z * 0.02);
    mGeom->setBody(mBody);
    enemy_entity->setUserObject(mGeom);
    //mGeom->setUserObject(static_cast<CollisionTestedObject*>(this));
    mBody->setPosition(mPosition);
	
/*
	enemy_node = mSceneMgr->getRootSceneNode()->createChildSceneNode();
	enemy_entity = mSceneMgr->createEntity("ship" + mName, "razor.mesh");
	enemy_node->attachObject(enemy_entity);
	mBody = new OgreOde::Body(mWorld, mName);
    enemy_node->attachObject(mBody);
    enemy_node->setScale(size.x * 0.02,size.y * 0.02,size.z * 0.02);
    mGeom->setBody(mBody);
    enemy_entity->setUserObject(mGeom);
    mBody->setPosition(mPosition);

	BoidAirCraftPtr AirCraftBoid(new BoidAirCraft(mBody,
		0.5f, // thrust
		0.3f, // lift
		0.25f, // minDrag
		0.8f, // maxDrag
		-0.15f, // gravity
		Ogre::Radian(0.05f), // max pitch
		Ogre::Radian(0.05f) // max roll
		));

	AirCraftBoid->setSightRange(1.0f, 10.0f, Ogre::Math::PI * 0.75f);

	BehaviorSeparation *separationBehavior = (BehaviorSeparation *)AirCraftBoid->addBehavior(BEHAVIOR_SEPARATION);
	separationBehavior->setDetectionRange(3.0f, 5.0f, (Ogre::Math::PI * 0.75f));
	separationBehavior->mWeight = 3.0f;

	BehaviorCohesion *cohesionBehavior = (BehaviorCohesion *)AirCraftBoid->addBehavior(BEHAVIOR_COHESION);
	cohesionBehavior->setDetectionRange(3.0f, 10.0f, Ogre::Math::HALF_PI);

	BehaviorAlignment *alignmentBehavior = (BehaviorAlignment *)AirCraftBoid->addBehavior(BEHAVIOR_ALIGNMENT);
	alignmentBehavior->setDetectionRange(3.0f, 8.0f, Ogre::Math::HALF_PI / 2.0f);

	BehaviorSeek *seekBehavior = (BehaviorSeek *)AirCraftBoid->addBehavior(BEHAVIOR_SEEK);
	seekBehavior->mWeight = 0.0f;
	mAirCraftBoids.push_back(CraftBehaviorPair(AirCraftBoid, seekBehavior));*/
}


Vector3 Enemy::get_position() const
{
	return mBody->getPosition();
}

void Enemy::update_position(Vector3 update)
{
	//enemy_node->translate(update);
    mBody->setLinearVelocity(10*update);
}



